package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Lff
 * @date 2022/3/2
 */
@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    /**
     * 分页
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(Integer page, Integer pageSize, String name) {
        Page<SetmealDto> pages = setmealService.findAllByPage(page, pageSize, name);
        return R.success(pages);
    }

    /**
     * 保存套餐信息
     *
     * @return
     */
    @CacheEvict(value = "setmealCache",allEntries = true)
    @PostMapping
    public R<String> saveSetmeal(@RequestBody SetmealDto setmealDto) {
        setmealService.addSetmealWithDish(setmealDto);
        return R.success("添加套餐成功！");
    }

    /**
     * 查询单个套餐的内部信息
     *
     * @return
     */
    @GetMapping("/{id}")
    public R<SetmealDto> getOne(@PathVariable Long id) {
        SetmealDto setmealDto = setmealService.getOneById(id);
        return R.success(setmealDto);
    }

    /**
     * 修改套餐的数据
     */
    @CacheEvict(value = "setmealCache",allEntries = true)
    @PutMapping
    public R<String> updateSetmeal(@RequestBody SetmealDto setmealDto) {
        setmealService.updateSetmealWithDish(setmealDto);
        return R.success("修改套餐成功！");
    }

    /**
     * 删除套餐
     */
    @DeleteMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R<String> deleteByIds(Long[] ids) {
        setmealService.deleteByIds(ids);
        return R.success("删除套餐成功！");
    }


    /**
     * 修改套餐状态
     */
    @PostMapping("/status/{status}")
    public R<String> updateStatusByIds(@PathVariable Integer status, Long[] ids) {
        setmealService.updateStatus(status,ids);
        return R.success("套餐状态修改成功！");
    }

    /**
     * 查看指定分类下的菜品信息
     */
    @GetMapping("/list")
    @Cacheable(value = "setmealCache", key = "'setmeal_'+ #setmeal.categoryId + '_cache'")
    public R<List> getSetmealList(Setmeal setmeal){
       List<Setmeal> setmealList = setmealService.getList(setmeal);
        return R.success(setmealList);
    }
}
